﻿using Dapper.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Enums;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Op.Common;
using YouWei.Utility;
namespace YouWei.Op.Controllers
{
    /// <summary>
    /// 微信用户行为轨迹
    /// </summary>
    public class WeChartTrailController : BaseController
    {
        WeChartTrailService wctService = new WeChartTrailService();
        CommonEnumService ceService = new CommonEnumService();

        // GET: WeChartTrail
        public ActionResult Index()
        {
            ViewBag.OpenID = GetParam("openid");
            List<CommonEnum> enums = ceService.QueryAll().OrderBy(t => t.EnumField).ToList();
            return View(enums);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ContentResult List(WeChatTrailSearchModel model)
        {
            int pageIndex = GetPageIndex();
            int pageSize = GetPageSize();
            Page<WeChartTrail> page = wctService.Pages(pageIndex, pageSize, model);
            Page<WeChatTrailPageModel> newPage = new Page<WeChatTrailPageModel>();
            var list = new List<WeChatTrailPageModel>();
            //处理枚举字段
            page.rows.ForEach(item =>
            {
                var p = new WeChatTrailPageModel();
                item.MergeTo(p);
                p.TrailTypeText = EnumHelper.GetEnumDesc(typeof(TrailTypeEnum), p.TrailType);
                p.ActionTypeText = EnumHelper.GetEnumDesc(typeof(ActionTypeEnum), p.ActionType);
                list.Add(p);
            });
            newPage.rows = list;
            newPage.total = list.Count;
            return ReturnResult(newPage);
        }
        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Detail(int userId)
        {
            WeChartTrail usr = wctService.Single(userId);
            if (usr == null)
            {
                usr = new WeChartTrail();
            }
            return View(usr);
        }
    }
}